package com.guet.niit.User.dao;

import com.guet.niit.User.pojo.Account;
import com.guet.niit.User.pojo.ChargingRecord;
import com.guet.niit.User.pojo.UserCar;
import com.guet.niit.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;

//任务：实现UserDao接口里的方法
/*
 * 连接数据库，并进行查询操作
 */

/**
 * 通过用户id返回相应的车牌号
 */
public class UserDaoImpl implements UserDao {
    @Override
    public ArrayList<UserCar> FindAllByUserId(Long UserId) {
//        String sql = "select license_number from  power.user_car where user_id=" + UserId;
        String sql = "select * from power.user_car where user_id=" + UserId;
        ResultSet rs = null;
        Connection connection = null;
        Statement statement = null;

        UserCar Car = null;

        ArrayList<UserCar> CarResult = new ArrayList<>();
        try {
            connection = JDBCUtil.createConnection();
            statement = connection.createStatement();
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                Long userId = rs.getLong("user_id");
                Long CarId = rs.getLong("car_id");
                String carName = rs.getString("car_name");
                String CarPlate = rs.getString("license_number");
                Integer carType = rs.getInt("car_type");
                String carPhoto = rs.getNString("car_photo");
                Car = new UserCar(userId,CarId,carName, CarPlate,carType,carPhoto);
                CarResult.add(Car);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.closeConnection(connection, statement, rs);
        }
        return CarResult;
    }

    /**
     * 通过用户id返回相应的余额
     *
     * @param UserId
     * @return
     */
    @Override
    public Double FindBalanceByUserId(Long UserId) {
        String sql = "select balance from  power.account where id=" + UserId;
        ResultSet rs = null;
        Connection connection = null;
        Statement statement = null;
        double Banlance = 0;
        try {
            connection = JDBCUtil.createConnection();
            statement = connection.createStatement();
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                Banlance = rs.getDouble(1);
            }
            Account account = new Account();
            account.setBalance(Banlance);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return Banlance;
    }

    /**
     * 通过用户id去更新充值之后的余额
     *
     * @param Id
     * @param Balance
     */
    @Override
    public int UpdateBalanceByUserId(Long Id, Double Balance) {
        String sql = "update power.account set balance=" + Balance + "where id=" + Id;
        int count;
        try {
            Connection connection = JDBCUtil.createConnection();
            Statement statement = connection.createStatement();
//            int count = statement.executeUpdate(sql);
            count = statement.executeUpdate(sql);
            System.out.println(count + "已更新");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return count;
    }

    public ArrayList<ChargingRecord> FindChargingRecordByUserId(Long UserId) {
        String sql = "select * from charging_record where user_id=" + UserId;
        ResultSet rs = null;
        Connection connection = null;
        Statement statement = null;

        ChargingRecord chargingRecord = null;

        ArrayList<ChargingRecord> Result = new ArrayList<>();
        try {
            connection = JDBCUtil.createConnection();
            statement = connection.createStatement();
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                Long Id = rs.getLong("id");
                Long userid = rs.getLong("user_id");
                Double cost = rs.getDouble("cost");
                Date starttime = rs.getDate("start_time");
                Date endtime = rs.getDate("end_time");
                int runtime = rs.getInt("run_time");
                int status = rs.getInt("status");
                Long carid = rs.getLong("car_id");
                chargingRecord = new ChargingRecord(Id, userid, cost, starttime, endtime, runtime, status, carid);
                Result.add(chargingRecord);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.closeConnection(connection, statement, rs);
        }
        return Result;
    }

    @Override
    public String findPasswordByUserId(Long UserId) {
        String sql = "select password from power.common_user where id=" + UserId;
        ResultSet rs = null;
        Connection connection = null;
        Statement statement = null;
        String Password = null;
        try {
            connection = JDBCUtil.createConnection();
            statement = connection.createStatement();
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                Password = rs.getString("password");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Password;
    }

    @Override
    public int updatePasswordByUserId(Long Id, String password) {
        String sql = "update power.common_user set password=? where id="+Id;
        int count;
        try {
            Connection connection = JDBCUtil.createConnection();
//            防SQL注入
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,password);
            count = preparedStatement.executeUpdate();
            System.out.println(count + "已更新");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return count;
    }
}
